
import requests
import unittest
import json
from parameterized import parameterized
from api.LoginApi import LoginApi
import app

def build_data():
    file = app.BASE_DIR+"\data\login.json"
    test_data = []
    with open(file,encoding="utf-8") as f:
        json_data = json.load(f)
        for case_data in json_data:
            username = case_data.get("username")
            password = case_data.get("password")
            verify_code = case_data.get("verify_code")
            content_type = case_data.get("content_type")
            status_code = case_data.get("status_code")
            status = case_data.get("status")
            msg = case_data.get("msg")
            test_data.append((username,password,verify_code,content_type,status_code,status,msg))
        return test_data
class TestLoginApi(unittest.TestCase):

    def setUp(self):
        self.login_api = LoginApi()
        self.session = requests.session()

    def tearDown(self):
        if self.session:
            self.session.close()

    @parameterized.expand(build_data)
    def test01_login(self,username,password,verify_code,content_type,status_code,status,msg):
        response = self.login_api.get_verify_code(self.session)
        self.assertEqual(status_code,response.status_code)
        self.assertIn(content_type,response.headers.get( "Content-Type"))

        response = self.login_api.login(self.session,username,password,verify_code)
        print("test01_login_success的login接口返回的数据：",response.json())
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(status, response.json().get("status"))
        self.assertIn(msg, response.json().get("msg"))
